package controller

import (
	"hytx_manager/middleware/auth"
	"hytx_manager/models"
	"hytx_manager/pkg/setting"
	"strconv"

	"github.com/Unknwon/com"

	"github.com/gin-gonic/gin"
)

func GetVipCodeList(c *gin.Context) {
	code := c.DefaultQuery("code", "")
	page := com.StrTo(c.DefaultQuery("page", "1")).MustInt()
	limit := com.StrTo(c.DefaultQuery("limit", strconv.Itoa(setting.AppSetting.PageSize))).MustInt()

	data, paginate := models.GetVipCodeList(page, limit, code)

	render(c, gin.H{
		"paginate": paginate,
		"data":     data,
	})
}

func AddVipCode(c *gin.Context) {
	days := com.StrTo(c.DefaultPostForm("days", "365")).MustInt()
	adminId := auth.User(c).ID
	data := make(map[string]interface{})
	data["days"] = days
	data["adminId"] = adminId
	err := models.AddVipCode(data)
	if err != nil {
		fail(c, err.Error())
		return
	}
	success(c)
}
